package com.tommy.jiami.utils;

import android.content.Context;
import android.graphics.BitmapFactory;
import android.preference.PreferenceManager;
import android.view.WindowManager;
import android.widget.Toast;

import com.tommy.jiami.MyApplication;

import java.math.BigDecimal;

public class Utils {
    public static int getStatusBarHeight() {
        int result = 0;
        int resourceId = MyApplication.getApplication().getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            result = MyApplication.getApplication().getResources().getDimensionPixelSize(resourceId);
        }
        return result;
    }

    public static String getString(int id, Object... formatArgs) {
        return MyApplication.getApplication().getResources().getString(id, formatArgs);
    }

    public static void showToast(String str) {
        Toast.makeText(MyApplication.getApplication(), str, Toast.LENGTH_SHORT).show();
    }

    public static void showToast(int id, Object... params) {
        Toast.makeText(MyApplication.getApplication(), getString(id, params), Toast.LENGTH_SHORT).show();
    }

    public static String getUserPassword() {
        String tempStr = PreferenceManager
                .getDefaultSharedPreferences(MyApplication.getApplication())
                .getString("password", null);
        if (tempStr != null) {
            try {
                return Crypt.decryptString(tempStr, Constants.my_password);
            } catch (Exception e) {
                Toast.makeText(MyApplication.getApplication(), e.toString(), Toast.LENGTH_LONG).show();
            }
        }
        return null;
    }

    public static Context getContext() {
        return MyApplication.getApplication();
    }

    public static float round(float f, int n) {
        BigDecimal bd = new BigDecimal(f).setScale(2, BigDecimal.ROUND_HALF_DOWN);
        return Float.parseFloat(bd.toString());
    }

    public static double round(double d, int n) {
        BigDecimal bd = new BigDecimal(d).setScale(2, BigDecimal.ROUND_HALF_DOWN);
        return Double.parseDouble(bd.toString());
    }

    public static int getScreenHeight() {
        WindowManager manager = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
        return manager.getDefaultDisplay().getHeight();
    }

    public static int getScreenWidth() {
        WindowManager manager = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
        return manager.getDefaultDisplay().getWidth();
    }

    public static int computeSampleSize(BitmapFactory.Options options, int minSideLength, int maxNumOfPixels) {
        int initialSize = computeInitialSampleSize(options, minSideLength, maxNumOfPixels);
        int roundedSize;
        if (initialSize <= 8) {
            roundedSize = 1;
            while (roundedSize < initialSize) {
                roundedSize <<= 1;
            }
        } else {
            roundedSize = (initialSize + 7) / 8 * 8;
        }
        return roundedSize;
    }

    private static int computeInitialSampleSize(BitmapFactory.Options options, int minSideLength, int maxNumOfPixels) {
        double w = options.outWidth;
        double h = options.outHeight;
        int lowerBound = (maxNumOfPixels == -1) ? 1 : (int) Math.ceil(Math.sqrt(w * h / maxNumOfPixels));
        int upperBound = (minSideLength == -1) ? 128 : (int) Math.min(Math.floor(w / minSideLength), Math.floor(h / minSideLength));
        if (upperBound < lowerBound) {
            return lowerBound;
        }
        if ((maxNumOfPixels == -1) && (minSideLength == -1)) {
            return 1;
        } else if (minSideLength == -1) {
            return lowerBound;
        } else {
            return upperBound;
        }
    }

    public static String getCryptType(int type) {
        return type == Crypt.TYPE_ENCRYPT ? "加密" : "解密";
    }

    public static String getFileName(String path) {
        return path.substring(path.lastIndexOf("/") + 1);
    }

    public static boolean isPicture(String path) {
        return path.endsWith(".jpg") || path.endsWith(".jpeg")
                || path.endsWith(".png") || path.endsWith(".bmp");
    }

    public static boolean isAudio(String path) {
        return path.endsWith(".mp3") || path.endsWith(".m4a")
                || path.endsWith("wma") || path.endsWith("wav")
                || path.endsWith("ogg") || path.endsWith("mid");
    }

    public static boolean isVideo(String path) {
        return path.endsWith(".avi") || path.endsWith(".mpg")
                || path.endsWith(".mpeg") || path.endsWith(".dat")
                || path.endsWith(".rmvb") || path.endsWith("wmv")
                || path.endsWith(".3gp") || path.endsWith(".flv")
                || path.endsWith(".mp4");
    }
}
